package ru.spbu.math.android.cbrf.dataprovider.cbrf;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

import ru.spbu.math.android.cbrf.model.Rate;
import ru.spbu.math.android.cbrf.model.RateType;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Class for parsing dynamic data from CBRF and put it in the List of Rate.
 * This class extends DefaultHandler
 * @author  Svetlana Zemlyanskaya
 */

//<Record Date="02.03.2001" Id="R01235">
//<Nominal>1</Nominal> 
//<Value>28,6200</Value> 
//</Record>

public class CurrencyDynamicSaxHandler extends DefaultHandler {
	
	private final String LOG_TAG = "CurrencyDynamicSaxHandler";
	
    private final String VALUE = "Value";
    private final String RECORD = "Record";
    private final String DATE = "Date";

    private List<Rate> currList;
    private Rate currency;
    private StringBuilder builder;
    private RateType rateType;


    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        builder.append(ch, start, length);
    }

    @Override
    public void endElement(String uri, String localName, String name)
            throws SAXException {
        if (this.currency != null) {
            if (localName.equalsIgnoreCase(VALUE)) {
            	String str = builder.toString().replace(",", ".").trim();
                currency.setValue(Double.parseDouble(str));
            } else if (localName.equalsIgnoreCase(RECORD)) {
                currList.add(currency);
            }

            builder.setLength(0);
        }
    }

    @Override
    public void startDocument() throws SAXException {
        currList = new ArrayList<Rate>();
        builder = new StringBuilder();
    }

    @Override
    public void startElement(String uri, String localName, String name,
                             Attributes attributes) throws SAXException {
        if (localName.equalsIgnoreCase(RECORD)) {
            this.currency = new Rate();
            String str = attributes.getValue(DATE);
            DateFormat df = new SimpleDateFormat("dd.MM.yyyy");
            Date date = new Date();
            try {
                date = df.parse(str);
            } catch (ParseException e) {
                date = Calendar.getInstance().getTime();
                Log.e(LOG_TAG, "CBRF.Parse Date Exception");
            }
            currency.setDate(date);
            currency.setResource(rateType);
        }
    }

    /**
	 * @return rate list from XML
	 */
    public List<Rate> getCurrency() {
        return currList;
    }
    
    /**
	 * Set target for CurrencyRate
	 * @param  rateType
	 */
    public void setResource(RateType rateType) {
    	this.rateType = rateType;
    }
}